package com.base.vistter.iframe.inverter.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.vistter.iframe.bean.Result;
import com.base.vistter.iframe.entity.SysJob;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.service.SysJobService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/sys/job")
public class SysJobController {

    @Resource(name = "sysJobService")
    private SysJobService sysJobService;

    @GetMapping
    public Result<Page<SysJob>> get(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageNum", defaultValue = "10") Integer pageNum, String code, String name) throws IframeException {
        Page<SysJob> page = Page.of(pageNo, pageNum);
        LambdaQueryWrapper<SysJob> lambdaQueryWrapper = new LambdaQueryWrapper<SysJob>();
        if (StringUtils.isNotEmpty(code)) {
            lambdaQueryWrapper.like(SysJob::getCode , code);
        }
        if (StringUtils.isNotEmpty(name)) {
            lambdaQueryWrapper.like(SysJob::getName , name);
        }
        return Result.ok(sysJobService.page(page, lambdaQueryWrapper));
    }

    @GetMapping("/{id}")
    public Result<SysJob> get(@PathVariable("id") Integer id) throws IframeException {
        return Result.ok(sysJobService.getById(id));
    }

    @PostMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> save(@RequestBody SysJob sysJob) throws IframeException {
        return Result.ok(sysJobService.save(sysJob));
    }

    @PutMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> update(@RequestBody SysJob sysJob) throws IframeException {
        return Result.ok(sysJobService.update(sysJob));
    }
    @DeleteMapping(produces = "application/json;charset=utf-8")
    public Result<Integer> delete(@RequestBody List<String> ids) throws IframeException {
        return Result.ok(sysJobService.delete(ids));
    }
    @DeleteMapping("/{id}")
    public Result<Integer> delete(@PathVariable("id") Integer id) throws IframeException {
        return Result.ok(sysJobService.delete(id));
    }
    @GetMapping("/status")
    public Result<Boolean> status(@RequestParam("id") Integer id, @RequestParam("status") Integer status) throws IframeException {
        SysJob sysJob = new SysJob();
        sysJob.setId(id);
        sysJob.setStatus(status);
        return Result.ok(sysJobService.updateById(sysJob));
    }
    @GetMapping("/list")
    public Result<List<SysJob>> getList() throws IframeException{
        LambdaQueryWrapper<SysJob> lambdaQueryWrapper = new LambdaQueryWrapper<SysJob>();
        lambdaQueryWrapper.eq(SysJob::getStatus  ,0);
        return Result.ok(sysJobService.selectList(lambdaQueryWrapper));
    }
}
